package com.vlin.api.client.request.tuya;

import com.vlin.api.client.Params;
import com.vlin.api.client.request.ParamsProducer;
import com.vlin.api.util.StringUtils;
import com.vlin.codec.HMACSHA256;
import com.vlin.constant.API;
import com.vlin.constant.TuyaAPIConstant;
import com.vlin.token.Token;

public class TuyaParamsProducer implements ParamsProducer {


	@Override
	public boolean support(API api) {
		
		return API.TUYA.equals(api) || API.TUYA == api;
	}

	@Override
	public Params produce(API api) {
		
		return produce(api, "");
	}

	@Override
	public Params produce(API api, Token token) {
		
		return produce(api, token.getToken());
	}

	@Override
	public Params produce(API api, String token) {
		Params params = new Params();
		long t = System.currentTimeMillis();
		String data = (StringUtils.isBlankSpace(token) ? TuyaAPIConstant.CLIENT_ID + t : TuyaAPIConstant.CLIENT_ID + token + t);
		String sign = HMACSHA256.sha256_HMAC(data, TuyaAPIConstant.SECRET).toUpperCase();
		params.setHeadParam("client_id", TuyaAPIConstant.CLIENT_ID);
		params.setHeadParam("sign", sign);
		params.setHeadParam("sign_method", TuyaAPIConstant.SIGN_METHOD);
		params.setHeadParam("t", String.valueOf(t));
		if(StringUtils.isBlankSpace(token)) {
			params.setParam("grant_type", "1");
		} else {
			params.setHeadParam("access_token", token);
		}
		
		return params;
	}

}
